const SERVICE = require("../../lib/js/service.js");

Page({
  data: {
    showPupop: false,    //显示城市    
    pickerVal: [0, 0],    //picker下标
    CityVal:null,   //城市
    showPlateNumbers: false,    //显示省份
    sheng: ["京", "沪", "浙", "苏", "粤", "鲁", "晋","关闭", "冀",
      "豫", "川", "渝", "辽", "吉", "黑", "皖", "鄂",
      "津", "贵", "云", "桂", "琼", "青", "新", "藏",
      "蒙", "宁", "甘", "陕", "闽", "赣", "湘"],
    shengVal: null,    //省份
    InputStatus:true,
    InputStatus_2: true ,
    hphmVal:null  
  },
  onLoad(options) {
    wx.showNavigationBarLoading();
    let _this  = this;
    SERVICE.POST(SERVICE.URL.getSupportCity,{},(status,res)=>{
      wx.hideNavigationBarLoading();
      console.log(res);
      if (res.data.result){
        _this.setData({
          city: res.data.result
        });
      }else{
        wx.showToast({
          title: res.data.message,
          icon:"none",
          mask:true
        });
      }
    });
  },
  /*分享*/
  onShareAppMessage() {

  },

  /*城市*/ 
  pickerChange(e){
    let td = this.data;
    if (e.detail.value[0] != td.pickerVal[0]){
      td.pickerVal[1] = 0;
      td.pickerVal[0] = e.detail.value[0];            
    }else{
      td.pickerVal[1] = e.detail.value[1];
    }
    this.setData({
      pickerVal: td.pickerVal
    });
  },

  /*取消*/ 
  pupopCancel(){
    this.setData({
      showPupop:false
    });
  },

  /*确定*/ 
  pupopConfirm(){
    let td = this.data, city = td.city, pickerVal = td.pickerVal;
    this.setData({
      showPupop: false,
      CityVal: city[pickerVal[0]].province +'/'+ city[pickerVal[0]].citys[pickerVal[1]].city_name,
      InputStatus: city[pickerVal[0]].citys[pickerVal[1]].class && true,
      InputStatus_2: city[pickerVal[0]].citys[pickerVal[1]].engine && true      
    });
  },
  
  getCity(){
   let td = this.data;
    console.log(td.city[td.pickerVal[0]].citys[td.pickerVal[1]]);
   if(td.city){
     this.setData({
       showPupop: true,
       is_engine: td.city[td.pickerVal[0]].citys[td.pickerVal[1]].engine,
       is_class: td.city[td.pickerVal[0]].citys[td.pickerVal[1]].class,       
       engineno: td.city[td.pickerVal[0]].citys[td.pickerVal[1]].engineno,
       classno: td.city[td.pickerVal[0]].citys[td.pickerVal[1]].classno
     });
   }else{
     this.onLoad();
   }
  },
  province(){
    this.setData({
      showPlateNumbers: true
    });
  },
  colse(){
    console.log("yes");    
    this.setData({
      showPlateNumbers: false
    });
  },
  getSheng(e){
    this.setData({
      showPlateNumbers: false,
      shengVal:e.currentTarget.dataset.sh
    });
  },
  inputChange(e){
    let td = this.data, val = e.detail.value;
    let hphmVal = "";
    for (let i of val) {
      if (!/[\u4e00-\u9fa5]/.test(i)) {
        hphmVal += i;
      }
    }
    this.setData({
      hphmVal: e.detail.value.toUpperCase()
    });
  },
  
  formSubmit(e){
    let td = this.data, form_obj = e.detail.value, 
    city = td.city[td.pickerVal[0]].citys[td.pickerVal[1]].city_code;
    if (!td.shengVal){
      wx.showToast({
        title: "请选择省份",
        icon:"none",
        mask:true
      });
    } else if (!td.hphmVal){
      wx.showToast({
        title: "请输入车牌号",
        icon: "none",
        mask: true
      });
    } else if (!td.CityVal){
      wx.showToast({
        title: "请选择城市",
        icon: "none",
        mask: true
      });
    } else if (td.is_engine == 1 && form_obj.engine.length < (td.engineno == 0 ? 1 : td.engineno)) {
      wx.showToast({
        title: "请输入完整发动机号",
        icon: "none",
        mask: true
      });
      
    } else if (td.is_class == 1 && form_obj.vin.length < (td.classno == 0 ? 17 : td.classno)) {
      wx.showToast({
        title: "请输入完整车架号",
        icon: "none",
        mask: true
      });
    }else{
      wx.showLoading({
        title: "查询中...",
        mask:true
      });
      SERVICE.POST(SERVICE.URL.getWz, {
        city: city,
        hphm: td.shengVal + form_obj.hphm,
        hpzl: "02",
        engineno: form_obj.engine,
        classno: form_obj.vin
      }, (status, res) => {
        wx.hideLoading();
        console.log(res);
        wx.navigateTo({
          url: "../weizhangchaxun-app-record/weizhangchaxun-app-record?violation=" + JSON.stringify(res.data),
        })
      });
    }
  },
  plateNumbers(){
    this.setData({
      showPlateNumbers: false
    });
  }, 
  input(e){
    let val = e.detail.value;
  },
  input2(e){
    let val = e.detail.value;
    let val_2 = "";
    for (let i of val) {
      if (!/[\u4e00-\u9fa5]/.test(i)) {
        val_2 += i;
      }
    }
    this.setData({
      SearchVal2: val_2.toUpperCase(),
    });
  }
});